﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.UI.Controls.AssistantMethods.StaticMethods
{
    public class CommonUse
    {

        /// <summary>
        /// 接口/类属性映射至实体属性
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="destination">目标</param>
        public static void AutoMapProperties(object? source, object destination)
        {
            if (source == null) return;
            PropertyInfo[] sourceProperties = source.GetType().GetProperties();
            PropertyInfo[] destinationProperties = destination.GetType().GetProperties();

            foreach (var sourceProp in sourceProperties)
            {
                foreach (var destProp in destinationProperties)
                {
                    if (sourceProp.Name == destProp.Name && sourceProp.PropertyType == destProp.PropertyType && destProp.CanWrite)
                    {
                        destProp.SetValue(destination, sourceProp.GetValue(source));
                        break;
                    }
                }
            }
        }
    }
}
